<?php
class StocksController extends AppController {
	var $name = 'Stocks';

    public function admin_update () {
        $data = $this->params['form'];

        if (isset($data)) {
            $adj  = array();
            $type = $date = $time = $remark = null;
            $stockist_id = 1;

            foreach ($data as $fieldname=>$value) {
                if (substr($fieldname, 0, 2) == 'p_' && !empty($value)) {
                    $id       = substr($fieldname, 2);
                    $adj[$id] = $value;
                } else if ($fieldname == 'stockist_id' && $value) {
                    $stockist_id = $value;
                } else if ($fieldname == 'type' && $value) {
                    $type = $value;
                } else if ($fieldname == 'date' && $value) {
                    $date = $value;
                } else if ($fieldname == 'time' && $value) {
                    $time = $value;
                } else if ($fieldname == 'remark' && $value) {
                    $remark = $value;
                }
            }

            if (count($adj) == 0) {
                $json = array(
                    'status' => false,
                    'mesg'   => __('No any product adjustment found', true)
                );
                $this->response($json);
                return;
            }

            $t = $this->clientToServerTime($date.' '.$time);
            $d = array();
            $i = 0;
            foreach ($adj as $id=>$qty) { // prepare to save all products adjustment at once
                $d[$i] = array(
                    'stockist_id' => $stockist_id,
                    'product_id'  => $id,
                    'type'        => $type,
                    'datetime'    => $t,
                    'in_out'      => $qty,
                    'remark'      => $remark
                );
                $i++;
            }

            if ($this->Stock->saveAll($d, array('validate'=>'first'))) { // validate all data before any are saved
                $mesg = __('Products adjustment have been updated successfully!',true);
                $json = array(
                    'status' => true,
                    'mesg'   => $mesg
                );
            } else { // probably invalid data found
                $json  = array('status' => false);
                $error = '';
                foreach ($this->Stock->invalidFields() as $k=>$v) {
                    if (is_string($k)) {
                        $error .= '<br />- '.$v;
                    }
                }
                if ($error) {
                    $json['mesg'] = __('Please check the following error and try again!', true).$error;
                }
            }
        } else {
            $json = array(
                'status' => false,
                'mesg'   => __('no data posted', true)
            );
        }

        $this->response($json);
    } // admin_update

    public function admin_index ($id=null) {
        if ($id) {
            $this->loadModel('Stockist');
            $this->Stockist->id = $id;
            $name = $this->Stockist->field('name');
        } else {
            $id   = 1;
            $name = 'AGAPE Head Office';
        }

        $data = $this->Stock->listing($id);

        $this->stylesheet  = am($this->stylesheet, 'jscal2/jscal2', 'jscal2/border-radius', 'clockpick');
        $this->javascripts = am($this->javascripts, 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', 'jscal2', 'clockpick.min', 'admin_stock');
        $this->pageTitle   = __('Stocks Adjustment', true);

        $this->set('pageTitle', $this->pageTitle);
        $this->set('data', $data);
        $this->set('stockist_id', $id);
        $this->set('stockist', $name);
    } // admin_index
} // StocksController
?>